//
//  WaitToSendViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/20.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "WaitToSendViewController.h"
#import "WaitToSendCell.h"
#import "WaitToSendHeadView.h"
#import "WaitToSendFootView.h"
#import "ResonView.h"
@interface WaitToSendViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)ResonView *resonView;
@end

@implementation WaitToSendViewController
-(ResonView *)resonView{
    if(_resonView==nil){
        _resonView =[[[NSBundle mainBundle]loadNibNamed:@"ResonView" owner:self options:nil]lastObject];
        _resonView.centerX = Main_Screen_Width/2;
        _resonView.centerY = self.view.centerY;
    }
    return _resonView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
     [self tableViewAndHeaderView];
    if(!ISNOTNEEDMESSAGEBTN){
        self.isNeedMessageItem = YES;
    }
    [self loadData];
}

-(void)loadData{
    //2.36查看待发货接口
    NSDictionary *paraDic = @{
                              @"userId":USERID,
                              @"state":@""
                              };
    __weak typeof(self)  weakSelf = self;
    [MBProgressHUD showLoadingGif];
    [Tools AFNetworkingRequestTypePostWithUrl:GETMYUNPAY paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"待发货接口---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *data = resultDic[@"data"];
            if([data isKindOfClass:[NSNull class]]){
                [MBProgressHUD dismissHUD];
                return ;
            }
            for(NSDictionary *dic  in data){
                WaitToSendGroup *group = [[WaitToSendGroup alloc]init];
                group.storeId = dic[@"storeId"];
                group.storeName = dic[@"storeName"];
                group.oId = dic[@"oId"];
                group.oPayTime = dic[@"oPayTime"];
                group.oSendTime = dic[@"oSendTime"];
                NSArray *products= dic[@"products"];
                NSMutableArray *waitToSendModels = [NSMutableArray array];
                for(NSDictionary *pDic in products){
                    WaitToSendModel *model = [[WaitToSendModel alloc]init];
                    model.pPic =pDic[@"pPic"];
                    model.pName =pDic[@"pName"];
                    model.pPrice =pDic[@"pPrice"];
                    model.pProperty =pDic[@"pProperty"];
                    model.pNum =pDic[@"pNum"];
                    [waitToSendModels addObject:model];
                    group.waitToSendModels = waitToSendModels;
                }
                [weakSelf.dataSource addObject:group];
            }
            if(!weakSelf.dataSource.count){
                [MBProgressHUD dismissHUD];
                [weakSelf.view addSubview:weakSelf.noDataIUmageV];

                return;
            }
            [weakSelf.tableV reloadData];
            weakSelf.tableV.hidden = NO;
            [MBProgressHUD dismissHUD];
        }else{
            [MBProgressHUD dismissHUD];
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}


- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight) style:UITableViewStyleGrouped];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.backgroundColor = DEF_COLOR242;
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableV];
    _tableV.tableFooterView = [[UIView alloc]init];
    _tableV.hidden = YES;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WaitToSendGroup *group = self.dataSource[section];
    return group.waitToSendModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ident=@"actell";
    WaitToSendCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WaitToSendCell" owner:self options:nil]lastObject];
    }
    WaitToSendGroup *group = self.dataSource[indexPath.row];
    WaitToSendModel *model = group.waitToSendModels[indexPath.row];
    cell.model = model;
    //退款
    __weak typeof(self)  weakSelf = self;
    cell.refundBlock= ^(){
        [weakSelf showResonViewWithOld:group.oId producrId:model.productId];
    };

    return cell;
}
-(void)showResonViewWithOld:(NSString*)old producrId:(NSString*)pid{
    UIWindow *window = (UIWindow*)[UIApplication sharedApplication].keyWindow;
    //添加遮罩视图
    [window addSubview:self.maskV];
    //添加红包视图
    [window addSubview:self.resonView];
    //打开遮罩视图的手势交互
    self.maskV.userInteractionEnabled = YES;
    __weak typeof (self) weakSelf = self;
    //点击红包视图左上角的关闭按钮回调处理
    self.resonView.closeBlock=^{
        [weakSelf.maskV removeFromSuperview];
        [weakSelf.resonView removeFromSuperview];
};
    
    self.resonView.commitBlock = ^(NSString *type,NSString*reason){
        //2.39退款接口
        NSDictionary *paraDic = @{
                                  @"userId":USERID,
                                  @"orderId":old,
                                  @"refundType":type,
                                  @"refundReson":reason,
                                  @"productId":pid,
                                  };
        [Tools AFNetworkingRequestTypePostWithUrl:REFUND paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
            DLog(@"退款接口-----%@",resultDic);
            if([resultDic[@"flag"] isEqualToString:@"true"]){
                NSDictionary *dataDic = resultDic[@"data"];
                if([dataDic isKindOfClass:[NSNull class]]) return ;
                if([dataDic[@"status"] isEqualToString:@"true"]){
                    [MBProgressHUD showTipMessageInWindow:@"'退款申请提交成功"];
                    [weakSelf.maskV removeFromSuperview];
                    [weakSelf.resonView removeFromSuperview];
                }else{
                    if(dataDic[@"errorString"]){
                        [MBProgressHUD showTipMessageInWindow:dataDic[@"errorString"]];
                    }
                }
            }else{
                if(resultDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
                }
            }
        }];
    };
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 107;
}
//自定义头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *headID = @"head";
    WaitToSendHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headID];
    if(headView==nil){
        headView = [[[NSBundle mainBundle]loadNibNamed:@"WaitToSendHeadView" owner:self options:nil]lastObject];
    }
    headView.group = self.dataSource[section];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 32;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSString *headID = @"footer";
    WaitToSendFootView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headID];
    if(footerView==nil){
        footerView = [[[NSBundle mainBundle]loadNibNamed:@"WaitToSendFootView" owner:self options:nil]lastObject];
    }
    footerView.group = self.dataSource[section];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 38;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
